import { HTTP } from 'https.js'
let http = new HTTP()
var uploadImage = require('uploadFile.js');//Base64,hmac,sha1,crypto相关算法
const formatTime = ()=> {
  var todey = new Date();
  todey.setTime(todey.getTime());
  return  todey.getFullYear()+"-" + (todey.getMonth()+1 < 10 ? "0"+parseInt(todey.getMonth()+1): todey.getMonth()+1) + "-" + (todey.getDate() < 10 ? "0"+parseInt(todey.getDate()):todey.getDate())+" "+todey.getHours()+":"+todey.getMinutes()+":"+ todey.getSeconds();
}

const upimg =  function(name,that) {
  return  wx.chooseImage({
          count: 9,
          sizeType: ['original', 'compressed'],
          sourceType: ['album', 'camera'],
          success: function (res) {
            var tempFilePaths = res.tempFilePaths;
            var nowTime = formatTime(new Date());
            //支持多图上传
            for (var i = 0; i < res.tempFilePaths.length; i++) {
              //上传图片
              uploadImage(res.tempFilePaths[i], 'img', 'cbb/',
                function (result) {
                  
                  that.setData({
                    [name]: result,
                  });
                  wx.hideLoading();
                }
              )
            }
          }
        });
}

const upvideo = function (name, that) {
  return wx.chooseVideo({
         sourceType: ['album', 'camera'],
         maxDuration: 60,
         camera: 'back',
         success: function (res) {
            var tempFilePaths = res.tempFilePath;
            var nowTime = formatTime(new Date());
            //支持多图上传
              //上传图片
              uploadImage(res.tempFilePath, 'video', 'cbb/',
                
                function (result) {
                  console.log(res.tempFilePath)
                  http.request({
                    url: 'mini/v1/release/works',
                    methods:'post',
                    data: { url: result},
                    success: function (res) {
                      var arr = [{ url: result, is_check: 0, works_id: res.data.works_id }];
                      that.setData({
                        [name]: arr.concat(that.data[name])
                      });
                    }
                  })
                  wx.hideLoading();
                }
              )
          }
        });
}

//上传图片
const upphoto = function (name, that) {
  return  wx.chooseImage({
          count: 9,
          sizeType: ['original', 'compressed'],
          sourceType: ['album', 'camera'],
          success: function (res) {
            var tempFilePaths = res.tempFilePaths;
            var nowTime = formatTime(new Date());
            //支持多图上传
            for (var i = 0; i < res.tempFilePaths.length; i++) {
              //上传图片
              uploadImage(res.tempFilePaths[i], 'img', 'cbb/',
                function (result) {
                  http.request({
                    url: 'mini/v1/release/photo',
                    methods:'post',
                    data: { url: result},
                    success: function (res) {
                      var arr = [{ url: result, is_check: 0, works_id: res.data.works_id }];
                      that.setData({
                        [name]: arr.concat(that.data[name])
                      });
                    }
                  })
                }
              )
            }
          }
        });
}

function timestampToTime(timestamp) {
  var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
  var Y = date.getFullYear() + '-';
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  var D = date.getDate() < 10 ? '0' + date.getDate() +' ' : date.getDate() +' '
  var h = (date.getHours() < 10 ? '0' + (date.getHours()) : date.getHours()) + ':';
  var m = (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes()) + ':';
  var s = (date.getSeconds() < 10 ? '0' + (date.getSeconds()) : date.getSeconds());
  var times = Y + M + D + h + m + s;
  return times;
}

function share() {
  return {
    title: '加入火戏，成就自己的职业梦想',
    path: 'other/login/login',
    imageUrl: '../../image/share.jpg'
  }
}


const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}



module.exports = {
  formatTime: formatTime,
  upimg: upimg,
  upvideo: upvideo,
  timestampToTime: timestampToTime,
  share: share,
  upphoto:upphoto //个人企业上传图片
}
